package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.dto.SatisfactionDTO;
import com.tbit.uqbike.object.pojo.vo.SatisfactionVO;
import com.tbit.uqbike.object.pojo.vo.StatisticsVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.SatisfactionService;
import com.tbit.uqbike.webmanager.dao.core.SatisfactionDao;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 满意度调查
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024/5/10
 */
@Service
public class SatisfactionServiceImpl implements SatisfactionService {

    @Resource
    private SatisfactionDao satisfactionDao;


    /**
     * 分页查询满意度调查
     *
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @Override
    public Result<PageResult<SatisfactionVO>> queryPage(SatisfactionDTO satisfactionDTO) {
        PageInfo<SatisfactionVO> pageInfo = PageHelper.startPage(satisfactionDTO.getPage(), satisfactionDTO.getSize()).
                doSelectPageInfo(() -> satisfactionDao.queryPage(satisfactionDTO));
        return Result.success(new PageResult<>(satisfactionDTO.getPage(), satisfactionDTO.getSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList()));
    }

    /**
     * 满意度调查导出
     *
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @Override
    public Result<Object> export(SatisfactionDTO satisfactionDTO) {
        List<SatisfactionVO> satisfactionVoS = satisfactionDao.queryPage(satisfactionDTO);
        ExcelUtil<SatisfactionVO> util = new ExcelUtil<>(SatisfactionVO.class);
        JsonResult result = util.exportExcel(satisfactionVoS, "满意度调查信息");
        return Result.success(result.getData());
    }

    /**
     * 满意度调查统计
     *
     * @param satisfactionDTO satisfactionDTO
     * @return result
     */
    @Override
    public Result<List<StatisticsVO>> statistics(SatisfactionDTO satisfactionDTO) {
        List<StatisticsVO> statistics = satisfactionDao.statistics(satisfactionDTO);
        List<Integer> type = statistics.stream().map(StatisticsVO::getType).collect(Collectors.toList());
        IntStream.range(0, 3)
                .filter(i -> !type.contains(i))
                .mapToObj(i -> new StatisticsVO()
                        .setCount(0)
                        .setName(i == 0 ? "不满意" : i == 1 ? "满意" : "未填写")
                        .setProportion(0)
                        .setType(i))
                .forEach(statistics::add);
        return Result.success(statistics);
    }
}
